import BScroll from '@better-scroll/core'
import { onMounted, onUnmounted, Ref, ref } from 'vue'
import ObserveDom from '@better-scroll/observe-dom'
BScroll.use(ObserveDom)

export default (wrapper: Ref<null | HTMLElement>, options: any, emit: (event: 'scroll', ...args: any[]) => void) => {
    const scroll = ref<null | BScroll>(null)
    onMounted(() => {
        if (wrapper.value !== null) {
            // debugger
            scroll.value = new BScroll(wrapper.value, {
                ...options,
                observeDOM: true
            })

            if (options.probeType) {
                scroll.value.on('scroll', (position: {x: number, y: number}) => {
                    emit('scroll', position)
                })
            }
        }
    })
    onUnmounted(() => {
        scroll.value && scroll.value.destroy()
    })
    return {
        scroll
    }
}
